package com.kfh.ybracelet.location;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.kfh.ybracelet.R;
import com.kfh.ybracelet.adapter.SafeAreaAdapter;
import com.kfh.ybracelet.base.BaseActivity;
import com.kfh.ybracelet.bean.SafeAreaResult;
import com.kfh.ybracelet.net.HttpUtil;
import com.kfh.ybracelet.utils.Constant;
import com.kfh.ybracelet.utils.DensityUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * 安全区域页面
 * Created by Aaron on 15/8/4.
 */
public class SafeAreaActivity extends BaseActivity {

    private List<SafeAreaResult.AreaList> temList = new ArrayList<SafeAreaResult.AreaList>();
    private SafeAreaAdapter adapter;
    //    private String id;
    private Intent mIntent;
    private SwipeMenuListView lv_safe_area;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_safe_area);
//        mApplication.addActivity(this);
//        id = getIntent().getStringExtra("id");
        initWidget();

    }

    @Override
    protected void onResume() {
        super.onResume();
        getAreaList();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mApplication.cancelPendingRequests(getClass().getName());
    }

    //删除安全区域
    private void delArea(String itemId) {

        JSONObject j = new JSONObject();
        j.put("token", sp.getString(Constant.TOKEN, ""));
        j.put("id", sp.getString(Constant.CURRENTCHILDID, ""));
        j.put("areaId", itemId);

        HttpUtil.setPostRequest("加载中...", this, Constant.WEB_ROOT + Constant.DELAREA, j.toJSONString(),
                getClass().getName(), new HttpUtil.SuccessListener() {

                    @Override
                    public void setOnSuccessListener(String str) {
                        try {
                            SafeAreaResult safeAreaResult = JSONObject.parseObject(str, SafeAreaResult.class);
//                            if (safeAreaResult.code == Constant.STATUS_SUCCESS) {

                            showToast("删除成功");
                            getAreaList();

//                            } else {
//                                showToast(Constant.showErrorMsg(safeAreaResult.code, SafeAreaActivity.this));
//                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                            showToast(Constant.NODATA);
                        }

                    }
                }, null);

    }

    //获取安全区域列表
    private void getAreaList() {

        JSONObject j = new JSONObject();
        j.put("token", sp.getString(Constant.TOKEN, ""));
        j.put("id", sp.getString(Constant.CURRENTCHILDID, ""));

        HttpUtil.setPostRequest("加载中...", this, Constant.WEB_ROOT + Constant.GETAREALIST,
                j.toJSONString(), getClass().getName(), new HttpUtil.SuccessListener() {

                    @Override
                    public void setOnSuccessListener(String str) {
                        try {
                            SafeAreaResult safeAreaResult = JSONObject.parseObject(str, SafeAreaResult.class);
//                            if (safeAreaResult.code == Constant.STATUS_SUCCESS) {

                            if (safeAreaResult.areaList != null) {
                                temList.clear();
                                temList = safeAreaResult.areaList;
                                adapter.setData(temList);
                                adapter.notifyDataSetChanged();

                                lv_safe_area.invalidate();

                            } else {
                                showToast("请先添加安全区域");
                            }

//                            } else {
//                                showToast(Constant.showErrorMsg(safeAreaResult.code, SafeAreaActivity.this));
//                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                            showToast(Constant.NODATA);
                        }

                    }
                }, null);

    }

    private void initWidget() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        ImageView iv_add_fence = (ImageView) findViewById(R.id.iv_add_fence);

        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText(getResources().getString(R.string.safe_area));
        TextView tv_add = (TextView) findViewById(R.id.tv_add);
        tv_add.setText(getResources().getString(R.string.add_safe_fence));

        SwipeMenuCreator creator = new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {
//                // create "open" item
//                SwipeMenuItem openItem = new SwipeMenuItem(
//                        getApplicationContext());
////                 set item background
//                openItem.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9,
//                        0xCE)));
////                 set item width
//                openItem.setWidth(DensityUtil.dip2px(SafeAreaActivity.this, 30));
//                // set item title
//                openItem.setTitle("Open");
//                // set item title fontsize
//                openItem.setTitleSize(18);
//                // set item title font color
//                openItem.setTitleColor(Color.WHITE);
//                // add to menu
//                menu.addMenuItem(openItem);

                // create "delete" item
                SwipeMenuItem deleteItem = new SwipeMenuItem(getApplicationContext());
                // set item background
                deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9, 0x3F, 0x25)));
                // set item width
                deleteItem.setWidth(DensityUtil.dip2px(SafeAreaActivity.this, 80));
                // set item title
//                deleteItem.setTitle("删除");
//                deleteItem.setTitleSize(15);
//                deleteItem.setTitleColor(Color.WHITE);
                // set a icon
                deleteItem.setIcon(R.mipmap.ic_delete);

                // add to menu
                menu.addMenuItem(deleteItem);

            }
        };

        lv_safe_area = (SwipeMenuListView) findViewById(R.id.lv_safe_area);

        adapter = new SafeAreaAdapter(this, temList);
        lv_safe_area.setAdapter(adapter);

        // set creator
        lv_safe_area.setMenuCreator(creator);

        lv_safe_area.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                switch (index) {
                    case 0:
                        // delete

                        delArea(temList.get(position).id);

                        break;
//                    case 1:
//                        // open
//
//                        LogUtil.e("TAG", "item=====1");
//
//                        break;
                }
                // false : close the menu; true : not close the menu
                return false;
            }
        });

        //查看安全区域详情
        lv_safe_area.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                mIntent = new Intent(SafeAreaActivity.this, SafeAreaDetailActivity.class);
                Bundle bunder = new Bundle();
                bunder.putSerializable("safeArea", temList.get(i));
                mIntent.putExtras(bunder);
                startActivity(mIntent);

            }
        });

        iv_left.setOnClickListener(this);
        iv_add_fence.setOnClickListener(this);

    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {

            case R.id.iv_left:
                finish();
                break;

            case R.id.iv_add_fence://添加安全围栏
                mIntent = new Intent(this, SafeAreaAddActivity.class);
                mIntent.putExtra("id", sp.getString(Constant.CURRENTCHILDID, ""));
                startActivity(mIntent);
                break;

        }

    }
}
